import { Injectable } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { AppConfig } from "../config";
import { Http, Headers } from "@angular/http";
import { TurboperCategory } from "../entity/turboper-category";
import { TurboperCurrent } from "../entity/turboper-current.model";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class OperCategorysService {
  private readonly param = "operCategorys";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) { }
  getOperCategorysList() {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.json().data as TurboperCategory[])
      .catch(this.handleError);
  }
  getOperCategorysByTurbStatusValue(turbStatusValue: number): Promise<TurboperCategory[]> {
    const url = `${this.url}/turbStatusValue/${turbStatusValue}`;
    return this.http.get(url)
      .toPromise()
      .then(response => response.json().data as TurboperCategory[])
      .catch(this.handleError);
  }
  create(turboperCurrent: TurboperCurrent): Promise<TurboperCurrent> {
    return this.http.post(`http://192.168.15.3:8585/webapi/v1/operCurrents`, JSON.stringify(turboperCurrent))
      .toPromise()
      .then(res => {
        try {
          let result = res.json();
          switch (result.code) {
            case "200":
              return result.data as TurboperCurrent;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }
  delete(id: number): Promise<void> {
    const url = `http://192.168.15.3:8585/webapi/v1/operCurrents/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);

  }
  
  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }
}



// WEBPACK FOOTER //
// ./src/app/service/oper-categorys.service.ts